//
//  UserCenter.UpdateInfo.swift
//  Center
//
//  Created by 蒋晨成 on 2021/12/30.
//

import Center_OSS
import Center_RPC
import Foundation

public extension UserCenter {
    // 更新用户信息
    // 成功后重新获取用户信息
    static func updateBasicProfile(
        username: String? = nil,
        gender: Int64? = nil,
        signature: String? = nil,
        avatar: String? = nil,
        complete: @escaping (Result<Void, Error>) -> Void
    ) {
        var basicProfile = Azir_BasicProfile()
        var keys = [String]()
        if let avatar = avatar, !avatar.isEmpty {
            basicProfile.avatar = avatar
            keys.append("avatar")
        }
        if let username = username, !username.isEmpty {
            basicProfile.username = username
            keys.append("username")
        }
        if let gender = gender {
            basicProfile.gender = gender
            keys.append("gender")
        }
        if let signature = signature, !signature.isEmpty {
            basicProfile.signature = signature
            keys.append("signature")
        }

        RPCCenter.azir.updateBasicProfile(basicProfile: basicProfile, keys: keys) { result in
            switch result {
            case .success:
                getMyProfile { getMyProfileResult in
                    switch getMyProfileResult {
                    case .success:
                        complete(.success(()))
                    case .failure(let error):
                        complete(.failure(error))
                    }
                }
            case .failure(let error):
                complete(.failure(error))
            }
        }
    }

    static func changePassword(
        accountName: String,
        oldPassword: String,
        newPassword: String,
        complete: @escaping (Result<Void, Error>) -> Void
    ) {
        RPCCenter.azir.changePassword(accountName: accountName, oldPassword: oldPassword, newPassword: newPassword) { result in
            switch result {
            case .success:
                UserCenter.logger.info("changePassword success")
                complete(.success(()))
            case .failure(let error):
                UserCenter.logger.error("changePassword error: \(error)")
                complete(.failure(error))
            }
        }
    }

    static func getMyProfile(complete: @escaping (Result<Void, Error>) -> Void) {
        RPCCenter.azir.getUserProfile(targetGuid: guid) { result in
            switch result {
            case .success(let response):
                let userInfo = UserInfo(response: response.basicProfile)
                UserCenter.update(userInfo: userInfo)
                UserCenter.logger.info("getMyProfile success")
                complete(.success(()))
            case .failure(let error):
                UserCenter.logger.error("getMyProfile error: \(error)")
                complete(.failure(error))
            }
        }
    }
}

public extension UserCenter {
    static func updateAvatar(_ avatar: Data?, process: ((_ presenting: Float) -> Void)? = nil, complete: @escaping (Result<String, Error>) -> Void) {
        TencentCOSCenter.upload(imageData: avatar, process: process) { uploadResult in
            switch uploadResult {
            case .success(let url):
                updateBasicProfile(avatar: url) { updateResult in
                    switch updateResult {
                    case .success:
                        complete(.success(url))
                    case .failure(let error):
                        complete(.failure(error))
                    }
                }
            case .failure(let error):
                complete(.failure(error))
            }
        }
    }
}
